<?php


class Food extends CI_Model{
		
	public function get_food($id = FALSE){
		if ($id === FALSE){
			$query = $this->db->get('food');
			return $query->result_array();
		}

		$query = $this->db->get_where('food', array('id' => $id));
		return $query->row_array();
	}
	
	public function get_food_all(){
		$query = $this->db->get('food');
		return $query->result_array();
	}
	
	//Exclui os adicionados pelos usuarios 
	//chamda na hora de criar dieta e de ver todos alimentos
	public function get_food_measure_all(){
		
		$this->db->select('*');
		$this->db->from('food');
		$this->db->join('measure', 'measure.m_id = food.id_measure');
		$this->db->where(array('new_id_patient'=>0));
		$query = $this->db->get('');
		return $query->result_array();
	}
	
	public function get_food_measure_all_by_patient($id_patient){
		$this->db->select('*');
		$this->db->from('food');
		$this->db->join('measure', 'measure.m_id = food.id_measure');
		$this->db->where(array('new_id_patient'=>0));
		$this->db->or_where('new_id_patient = ',$id_patient);
		$query = $this->db->get('');
		return $query->result_array();
	}
	
	public function get_food_measure_by_id($id_food){
		$this->db->select('*');
		$this->db->from('food');
		$this->db->join('measure', 'measure.m_id = food.id_measure');
		$this->db->where(array('id' => $id_food));
		$query = $this->db->get('');
		return $query->row_array();
	}
	
	public function new_food(){
		if ($this->input->post('new_food')){
			$data = array(
				'name'		 => $this->input->post('name_food'),
				'id_measure' => (int) $this->input->post('measure'),
				'gml'		 => (int) $this->input->post('gml'),
				'kcal'		 => (int) $this->input->post('kcal'),
				'protein'	 => $this->input->post('protein'),
				'carb'		 => $this->input->post('carb'),
				'fat_t'		 => $this->input->post('fat_t'),
				'fat_s'		 => $this->input->post('fat_s'),
				'fat_tr'	 => $this->input->post('fat_tr'),
				'fiber'		 => $this->input->post('fiber'),
				'sodium'	 => $this->input->post('sodium')
			);

			$this->db->insert('food', $data);
			return 	$this->db->insert_id();
		}
	}
	
	public function upload_food(){
		if ($this->input->post('update_food')=='1'){
			$id_food		= $this->input->post('id_food');
			$data = array(
				'name'		 => $this->input->post('name_food'),
				'id_measure' => (int) $this->input->post('measure'),
				'gml'		 => (int) $this->input->post('gml'),
				'kcal'		 => (int) $this->input->post('kcal'),
				'protein'	 => $this->input->post('protein'),
				'carb'		 => $this->input->post('carb'),
				'fat_t'		 => $this->input->post('fat_t'),
				'fat_s'		 => $this->input->post('fat_s'),
				'fat_tr'	 => $this->input->post('fat_tr'),
				'fiber'		 => $this->input->post('fiber'),
				'sodium'	 => $this->input->post('sodium')
			);
			
			$this->db->where('id', $id_food);
			$this->db->update('food', $data); 

			return $this->db->insert_id();
		}
	}
	
	public function remove_food(){
		if ($this->input->post('remove_food')=='1'){
			$id_food		= $this->input->post('id_food');
			
			$this->db->where('id', $id_food);
			$this->db->delete('food'); 
		}
	}
	
	public function new_food_by_patient(){
		$data = array(
			'name'		 => $this->input->post('name'),
			'id_measure' => (int) $this->input->post('id_measure'),
			'gml'		 => (int) $this->input->post('gml'),
			'kcal'		 => (int) $this->input->post('kcal'),
			'protein'	 => $this->input->post('protein'),
			'carb'		 => $this->input->post('carb'),
			'fat_t'		 => $this->input->post('fat_t'),
			'fat_s'		 => $this->input->post('fat_s'),
			'fat_tr'	 => $this->input->post('fat_tr'),
			'fiber'		 => $this->input->post('fiber'),
			'sodium'	 => $this->input->post('sodium'),
			'new_id_patient' => (int) $this->input->post('new_id_patient') //se não for adicionado por paciente, passa zero
		);

		$this->db->insert('food', $data);
		return 	$this->db->insert_id();
	}
}


?>
